package io.github.junxworks.qt.modules.mm.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import io.github.junxworks.ep.core.orm.TBaseMapper;
import io.github.junxworks.qt.modules.mm.dto.MmDatastreamCondition;
import io.github.junxworks.qt.modules.mm.entity.MmDatastream;
import io.github.junxworks.qt.modules.mm.vo.FieldVo;
import io.github.junxworks.qt.modules.mm.vo.MmDatastreamVo;

@Mapper
public interface MmDatastreamMapper extends TBaseMapper<MmDatastream> {

	List<MmDatastreamVo> queryList(MmDatastreamCondition condition);
	
	@Select("select * from mm_datastream where id=#{id}")
	MmDatastreamVo queryMmDatastreamById(@Param("id") Long id);

	@Delete("delete from mm_ds_field_rela where ds_id=#{id}")
	int deleteDsFieldRela(Long dsId);

	@Select("select f.*,u.name `creatorName` from mm_ds_field_rela dfr,mm_field f left join ep_s_user u on f.creatorId=u.id where dfr.ds_id=#{id} and dfr.field_id=f.id order by f.id desc")
	List<FieldVo> queryFieldsByDsId(Long id);
	

	@Select("select * from mm_datastream where datastream_name=#{dsName} and status=0")
	MmDatastreamVo queryMmDatastreamByName(@Param("dsName") String dsName);

	@Select("select group_concat(name) `names` from mm_variable where ds_id=#{id} and status=0 group by ds_id")
	String queryVariableNamesByDsId(Long id);
	
	@Select("SELECT group_concat(e.exp_name) `names` FROM mm_exp_ds_rela edr, mm_expression e WHERE edr.ds_id = #{id} AND edr.exp_id = e.id AND e.status = 0 GROUP BY edr.ds_id")
	String queryExpNamesByDsId(Long id);
}